<template>
  <div>
    <el-menu :default-active="listQuery.tab" class="el-menu-demo" mode="horizontal" @select="changeTab">
      <el-menu-item index="department" v-if="departSaleTargetDataConsume">部门</el-menu-item>
      <el-menu-item index="all" v-if="saleTargetDataConsume">全部</el-menu-item>
    </el-menu>

    <div style="margin-top: 20px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="80px">
        <el-form-item label="销售日期：">
          <el-date-picker
            v-model="listQuery.targetDate"
            type="date"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="品类：">
          <el-select
            filterable
            clearable
            v-model="listQuery.goodsName">
            <el-option
              v-for="item in options2"
              :key="item.goodsName"
              :label="item.goodsName"
              :value="item.goodsName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="牌号：">
          <el-select v-model="listQuery.goodsKingId" filterable placeholder="请选择" clearable>
            <el-option
              v-for="item in goodsList"
              :key="item.kingId"
              :label="item.goodsSn"
              :value="item.kingId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="采购员：">
          <el-select size="mini" v-model="listQuery.purchaseUserId" clearable  placeholder="请选择人员" >
            <el-option v-for="item in userOptionList" :key="item.userId" :label="item.userName" :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            @click="handleSearchList()"
            type="primary">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="font-size:14px;padding:20px 0">实际销量：<span style="font-weight: bold">{{ actualTargetNumSummary }}</span>吨<span style="padding-left:20px;">销售目标销量：<span style="font-weight: bold">{{ saleTargetNumSummary }}</span>吨</span><span style="padding-left:20px">采购确认目标：<span style="font-weight: bold">{{ purchaseGoodsNumSummary }}</span></span></div>

    <!--数据列表-->
    <div class="table-container">
      <el-table
        v-loading="listLoading"
        :data="tableData"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column
          label="品类"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.goodsName}}
          </template>
        </el-table-column>
        <el-table-column
          label="牌号"
          min-width="150"
          align="center">
          <template slot-scope="scope">
            {{scope.row.goodsSn}}
          </template>
        </el-table-column>
        <el-table-column
          label="负责采购"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.purchaseUserName}}
          </template>
        </el-table-column>
        <el-table-column
          label="销售日期"
          min-width="150"
          align="center">
          <template slot-scope="scope">
            {{scope.row.targetDate}}
          </template>
        </el-table-column>
        <el-table-column
          label="货源数量"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.goodsNum}}
          </template>
        </el-table-column>
        <el-table-column
          class-name="cspantd"
          label="部门"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            <div v-for="(item,index) in scope.row.departmentList" style="line-height: 26px;height:26px" :style="{'border-top':index == 0 ? '0px solid #EBEEF5' : '1px solid #EBEEF5'}">{{item.departmentName}}</div>
            <div style="line-height: 26px;height:26px;border-top: 1px solid #EBEEF5" v-if="scope.row.departmentList.length > 1">合计</div>
          </template>
        </el-table-column>
        <el-table-column
          class-name="cspantd"
          label="销售设定目标"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            <div v-for="(item,index) in scope.row.departmentList" style="line-height: 26px;height:26px" :style="{'border-top':index == 0 ? '0px solid #EBEEF5' : '1px solid #EBEEF5'}">{{item.saleTargetNum}}</div>
            <div style="line-height: 26px;height:26px;border-top: 1px solid #EBEEF5" v-if="scope.row.departmentList.length > 1">{{ scope.row.departmentList | fifsum('saleTargetNum')}}</div>
          </template>
        </el-table-column>
        <el-table-column
          class-name="cspantd"
          label="采购确认目标"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            <div v-for="(item,index) in scope.row.departmentList" style="line-height: 26px;height:26px" :style="{'border-top':index == 0 ? '0px solid #EBEEF5' : '1px solid #EBEEF5'}">{{item.actionTargetNum}}</div>
            <div style="line-height: 26px;height:26px;border-top: 1px solid #EBEEF5" v-if="scope.row.departmentList.length > 1">{{ scope.row.departmentList | fifsum('actionTargetNum')}}</div>
          </template>
        </el-table-column>
        <el-table-column
          class-name="cspantd"
          label="操作"
          fixed="right"
          min-width="150"
          align="center">
          <template slot-scope="scope">
            <div v-for="(item,index) in scope.row.departmentList" style="line-height: 26px;height:26px" :style="{'border-top':index == 0 ? '0px solid #EBEEF5' : '1px solid #EBEEF5'}">
              <el-button type="text" @click="submit(item)" size="small" :loading="btnLoading2" v-if="scope.row.purchaseUserId == userId && item.actionTargetNum <= 0 && scope.row.targetDateTime >= nowDate0 " style="padding:0"> 同意目标 </el-button>
              <el-button type="text" @click="showPop(item)" size="small" v-if="scope.row.purchaseUserId == userId && item.actionTargetNum <= 0 && scope.row.targetDateTime >= nowDate0 " style="padding:0"> 修正目标 </el-button>
            </div>
            <div style="line-height: 26px;height:26px;border-top: 1px solid #EBEEF5"  v-if="scope.row.departmentList.length > 1"> </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>

    <!--采购确认目标-->
    <el-dialog
      :close-on-click-modal="false"
      title="采购修正目标"
      :visible.sync="dialogVisible"
      width="400px">
      <el-form :model="form" size="mini" label-width="110px" ref="form" :rules="rules">
        <el-form-item label="销售设定目标：">
           {{form.saleTargetNum}}
        </el-form-item>
        <el-form-item label="采购修正目标：" prop="actualTargetNum">
          <el-input v-model="form.actualTargetNum" style="width:220px" type="number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleConfirm" :loading="btnLoading">确 定</el-button>
      </span>
    </el-dialog>





  </div>
</template>
<script>
import {getCustomerOption,getOrderUsersList,saleTargetDetailList,saleTargetTargetList,orderGoodsList,getGoodsClassList} from '@/api/partner'
import { targetCheckList,targetCheck} from '@/api/allInLine/goods.js'
import {formatDate} from '@/utils/date';
import store from '@/store'
import { mapGetters } from 'vuex'
const defaultListQuery={
  "goodsKingId" : '',
  "targetDate" : '',
  "purchaseUserId" : '',
  "urgencyType" : "",
  goodsName:'',
  tab:'',
  "pageNum" : 1,
  "pageSize" : 10
};


const defaultForm={
  "checkId" : 0,
  saleTargetNum:'',
  actualTargetNum:''
}

export default {
  name: "pumsTargetConfirm",
  data(){
    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      listLoading: false,
      roles:store.getters.roles ? store.getters.roles : [],
      loading:false,
      count:0,
      activeName:'first',
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7
        }
      },
      userOptionList:[],
      dialogVisible:false,
      form:JSON.parse(JSON.stringify(defaultForm)),
      btnLoading:false,
      goodsList:[],

      rules:{
        actualTargetNum:[{required: true, message: '此项必填', trigger: 'blur'},],
      },
      title:'',
      nowDate0:0,
      btnLoading2:false,
      options2:[],
      actualTargetNumSummary:0,
      purchaseGoodsNumSummary:0,
      saleTargetNumSummary:0,
    }
  },
  props: {

    nowDate:{
      type: Number,
      default: () => parseInt(Date.parse(new Date())/1000)
    },
    startDate:{
      type: String,
      default: () => ''
    },
    endDate:{
      type: String,
      default: () => ''
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId',
      'isMange',
    ]),
    saleTargetDataConsume(){
      if(this.roles.indexOf("saleTargetDataConsume") != -1){
        return true
      }else{
        return false
      }
    },
    departSaleTargetDataConsume(){
      if(this.roles.indexOf("departSaleTargetDataConsume") != -1){
        return true
      }else{
        return false
      }
    },
  },
  created() {
    this.listQuery.targetDate = formatDate(new Date(), 'yyyy-MM-dd');
    if(this.departSaleTargetDataConsume){
      this.listQuery.tab = 'department'
    }else if(this.saleTargetDataConsume){
      this.listQuery.tab = 'all'
    }
    this.getList();
    orderGoodsList().then((res)=>{
      this.goodsList = res.list
    })
    this.getUserList();
    let  date = new Date(); // 获取当前时间
    date.setHours(0, 0, 0, 0); // 将时、分、秒和毫秒设为 0
    this.nowdate0 = parseInt(date.getTime()/1000);
    this.remoteMethod3()
  },
  filters: {
    fifsum(list,key){
      let sum = 0
      list.forEach((el)=>{
          let item = el[key] ? parseFloat(el[key]) :0
          sum = sum + item
      })
      return sum.toFixed(4)
    }
  },
  methods:{
    changeTab(key){
      this.listQuery = JSON.parse(JSON.stringify(defaultListQuery));
      this.listQuery.tab = key;
      this.listQuery.targetDate = formatDate(new Date(), 'yyyy-MM-dd');
      this.handleSearchList()
    },
    //获取品类列表
    remoteMethod3() {
      getGoodsClassList().then((res)=>{
        this.options2 = res;
      }).catch(()=>{
      });
    },
    submit(item){
      let data = {};
      data.checkId = item.checkId
      data.actualTargetNum = item.saleTargetNum
      targetCheck(data).then((res)=>{
        this.btnLoading2 = false;
        this.getList();
      }).catch(()=>{
        this.btnLoading2 = false;
      })
    },

    getUserList(){
      getOrderUsersList({departmentId:3,status:1}).then((res)=>{
        this.userOptionList = res;
      })
    },

    showPop(item){
      this.dialogVisible = true;
      this.form.checkId = item.checkId
      this.form.saleTargetNum = item.saleTargetNum
      this.form.actualTargetNum = ''

    },

    handleClose(){
      this.dialogVisible = false;
    },
    handleConfirm(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          let data = JSON.parse(JSON.stringify(this.form));
          targetCheck(data).then((res)=>{
            this.btnLoading = false;
            this.handleClose();
            this.getList();
          }).catch(()=>{
            this.btnLoading = false;
          })
        }
      })
    },



    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getList();
    },

    //获取列表
    getList() {
      this.listLoading = true;
      let pa = JSON.parse(JSON.stringify(this.listQuery))
      targetCheckList(pa).then(response => {
        this.tableData = response.list;
        this.count = response.count;
        this.listLoading = false;
        this.actualTargetNumSummary = response.actualTargetNumSummary
        this.purchaseGoodsNumSummary = response.purchaseGoodsNumSummary
        this.saleTargetNumSummary = response.saleTargetNumSummary
      }).catch(()=>{
        this.listLoading = false;
      });
    },


  },
}
</script>

<style scoped lang="scss">

</style>
